package com.awei.study.annotation.InitBinder;


import com.awei.study.controller.BaseController;
import com.awei.study.domain.R;
import com.awei.study.domain.User;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/initBinder")
public class InitBinderTest extends BaseController {


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class,new PropertyEditorSupport(){
            @SneakyThrows
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                setValue(sdf.parse(text));
            }
        });
    }

    @GetMapping("/test")
    public R test(User user) {
//        System.out.println(user.getName());
//        System.out.println(user.getDate());
//        System.out.println(user);
        log.info(user.getName());
        log.info(user.getDate().toString());
        return R.ok(user);
    }


}
